﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Designer.Event.NCL;
using CPPEI.Coolzon.Designer.Event.UI;
using CPPEI.Coolzon.Designtime.Metadata;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CPPEI.Coolzon.Designer.Views
{
    public partial class CreateSolutionView
    {
        [Subscribe]
        public void EndCreateSolution(EndCreateSolutionEvent @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                this.IsEnabled = true;
            });

            if (@event.State == NCLEventState.Failure)
            {
                this.Dispatcher.Invoke(() =>
                {
                    CustomMessageBox.ShowError("创建方案失败,方案名称可能重复。");
                    Logger.ErrorToTag("CreateSolutionView", "创建方案失败,error:{0}", @event.Error);
                });
            }
            else if(@event.State == NCLEventState.Finished)
            {
                if(solution != null)
                {
                    solution.ID = @event.Solution.Id;
                    LocalStorage.SaveSolutionFile(UIContext.Current.LocalWorkSpace.LocalPath, solution);
                    VSFileMgr.GoToAdd(UIContext.Current.LocalWorkSpace.LocalPath, solution.ID, solution.ID, Guid.Empty);

                    OpenSolutionThread.Start(solution.ID);
                    this.Dispatcher.Invoke(() =>
                    {
                        this.DialogResult = true;
                    });
                }
                else
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        CustomMessageBox.ShowWarning("solution为空");
                        Logger.ErrorToTag("CreateSolutionView", "EndCreateSolution,solution为空");
                    });
                }
            }
        }
    }
}
